<template>
  <div>
    <el-menu router default-active="1" class="Ecp-tab-menu" mode="horizontal">
      <el-menu-item route='/menu/webMenu' index="1">管理端菜单</el-menu-item>
      <el-menu-item route='/menu/supplyMenu' index="2">供应端菜单</el-menu-item>
    </el-menu>
    <div class="Sort">
      <div class="Sort__header">
        <div class="Sort__header__bar">
          <AddModuler menu-type='web' @on-success='val => addMenuModulerSuccess(val)' />
          <div class="Sort__search">
              <el-cascader
                @change="doSearch"
                clearable
                placeholder="搜索菜单"
                :options="searchOption"
                change-on-select
                filterable
                :debounce='800'
              ></el-cascader>
            <!-- <el-button style="margin-left:30px;" type="primary" size="mini" @click="doSearch">搜索</el-button> -->
          </div>
        </div>
        <div class="Sort__table__header">
          <span class="Sort__table__header__item">模块名称</span>
          <span class="Sort__table__header__item">资源类型</span>
          <span class="Sort__table__header__item">操作</span>
        </div>
      </div>
      <el-scrollbar ref='scroll' class="Sort__content">
        <el-tree
          ref='treenode'
          :data="menuTree"
          node-key="id"
          :props="{children:'childList'}"
          :filter-node-method="filterNode"
          :expand-on-click-node="false">
          <div class="Sort__tree" slot-scope="{ node, data }">
            <span class="Sort__tree__name">{{ data.name }}</span>
            <span class="Sort__tree__btns">
              <AddModuler v-if="data.parentId == 1" menu-type='web' :parent-id="data.id" @on-success='(val) => addMenuModulerSuccess(val, data)' />
              <UpdateMenu menu-type='web' :menu-id="data.id" @on-success='(val) => addMenuModulerSuccess(val, data)' />
              <!-- <el-button type="text">编辑</el-button> -->
              <el-button type="text" @click="deleteWebMenu(node, data)">删除</el-button>
            </span>
            <span class="Sort__tree__sortid"> {{ data.parentId==1?'菜单':'按钮' }}</span>
          </div>
        </el-tree>
      </el-scrollbar>
    </div>
  </div>
</template>

<script>
import {getWebMenuList, deleteWebMenu} from '~/api/'
import AddModuler from '~/components/menuMenager/addModuler.vue'
import UpdateMenu from '~/components/menuMenager/updateMenu.vue'
  export default {
    components: {
      AddModuler,
      UpdateMenu
    },
    async asyncData ({app}) {
      try {
        const res = await getWebMenuList(app)
        if (res.code == '0') {
          return {menuTree: res.data.tree, message: '获取成功'}
        } else {
          return {menuTree: [], message: res.message}
        }
      } catch (error) {
        console.error(`后台菜单管理数列表拉取失败，错误信息：${error}`)
      }
    },
    computed: {
      searchOption () {
        if (this.menuTree.length == 0) return []
        let searchArr = []
        this.menuTree.map(item => {
          let option = {value: item.id, label: item.name}
          if (item.childList&&item.childList.length > 0) {
            option['children'] = []
            item.childList.map(val => {
              option['children'].push({value: val.id, label: val.name})
            })
          }
          searchArr.push(option)
        })
        return searchArr
      }
    },
    data() {
      return {
        searchText: "",
        loading: false,
        props: {
          label: 'name',
          children: 'zones'
        },
        count: 2
      }
    },
    mounted() {
      this.$refs.scroll.$el.style.height = document.documentElement.clientHeight-101+'px'
      // this.getPurchaseCategoryList()
    },
    methods: {
      doSearch (val) {
        // this.$refs.treenode.setCurrentNode(val.length > 1 ? val[1] : val[0])
        this.$refs.treenode.filter(val.length > 1 ? val[1] : val[0])
      },
      filterNode(value, data) {
        // console.log(value, data)
        if (!value) return true;
        return data.id == value || data.parentId == value;
      },
      addMenuModulerSuccess (data, parentData = false) {
        if (parentData) return this.$refs.treenode.append(data, parentData)
        this.$refs.treenode.insertAfter(data, this.menuTree[this.menuTree.length-1])
        // this.menuTree.push({
        //   id: data['id']||11,
        //   name: data['name'],
        //   parentFullId: `${data['parentId']}-${data['id']||1}`,
        //   parentId: data['parentId']
        // })
      },
      async deleteWebMenu (node, data) {
        const res = await deleteWebMenu(this, data.id)
        if (res.code !== '0') return this.$alert(res.message, '提示', {type: 'error'})
        this.$message.success('删除成功')
        this.$refs.treenode.remove(data)
      }
    }
  }
</script>

<style lang="scss" scoped>

.Sort {
  &__search {
    float: right;
    & /deep/ {
      .el-cascader {
        line-height: 28px;
      }
      .el-input__inner {
        border-radius: 14px;
        background-color: #f0f5fb;
        height: 28px;
        line-height: 28px;
        font-size: 12px;
      }
      .el-input__icon {
        line-height: 28px;
      }
    }
  }
  &__tree {
    width: 100%;
    font-size: 16px;
    color: #000000;
    height: 40px;
    line-height: 40px;
    &__name {
      font-weight: bold;
    }
    &__btns {
      float: right;
      width: 256px;
      text-align: right;
    }
    &__sortid {
      float: right;
      width: 120px;
      text-align: center;
      margin-right: 150px;
    }
  }
  &__content {
    padding: 10px 90px 10px 45px;
    background-color: #f5f9fe;
    width: 100%;
    height: 600px;
    & /deep/ {
      .el-tree {
        background-color: #f5f9fe;
      }
      .el-tree-node__content {
        padding-top: 6px;
        padding-bottom: 6px;
        height: 52px;
        line-height: 40px;
      }
      .el-tree-node__expand-icon {
        vertical-align: middle;
      }
      .el-button--text {
        color: #000000;
        font-size: 16px;
        font-weight: bold;
        padding: 11px 16px;
        &:hover {
          color: #409eff;
        }
        &:disabled {
          color: #9c9c9c;
        }
      }
    }
  }
  &__table {
    &__header {
      font-size: 16px;
      padding: 0px 90px 11px 45px;
      text-align: center;
      height: 32px;
      position: sticky;
      top: 0;
      &__item {
        font-weight: bold;
        &:first-child {
          float: left;
          padding-left: 16px;
          position: relative;
          &:before {
            content: "";
            display: block;
            width: 6px;
            height: 6px;
            border-radius: 50%;
            background-color: #409eff;
            position: absolute;
            top: 50%;
            left: 0;
            margin-top: -3px;
          }
        }
        &:last-child {
          float: right;
        }
      }
    }
  }
  &__header {
    border-bottom: 1px solid #e4e7ed;
    &__bar {
      padding: 24px 90px 24px 47px;
      height: 76px;
    }
  }
}
</style>
